package com.termux.setup.connections

import com.termux.setup.SetupHelper
import com.termux.setup.SourceConnection
import java.io.IOException
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL

/**
 * @author kiva
 */

class NetworkConnection(private val sourceUrl: String) : SourceConnection {
    private var connection: HttpURLConnection? = null

    @Throws(IOException::class)
    override fun getInputStream(): InputStream {
        if (connection == null) {
            connection = openHttpConnection()
            connection!!.connectTimeout = 8000
            connection!!.readTimeout = 8000
        }
        return connection!!.inputStream
    }

    override fun getSize(): Int {
        return if (connection != null) {
            connection!!.contentLength
        } else 0

    }

    override fun close() {
        if (connection != null) {
            connection!!.disconnect()
        }
    }

    @Throws(IOException::class)
    private fun openHttpConnection(): HttpURLConnection {
        val arch = SetupHelper.determineArchName()

        return URL("https://bintray.com/termux/bootstrap/download_file?file_path=bootstrap-aarch64-v25.zip").openConnection() as HttpURLConnection
//        return URL("$sourceUrl/boot/$arch.zip").openConnection() as HttpURLConnection
    }
}
